package GUI;

import java.io.IOException;

import javax.swing.JOptionPane;
import Kernel.Administrateur;
import Kernel.Client;
import Kernel.Serveur;

public class Accueil extends javax.swing.JFrame {

	private static final long serialVersionUID = 6398015577103208361L;
	public static Serveur serveur;
	
	// Variables d'environnement de la GUI
	
	public static String versionUtilisation;

	/** Cree une nouvelle instance de Accueil */
	public Accueil() {
		initComponents();
		Accueil.serveur = new Serveur();
		Accueil.versionUtilisation = this.jComboBox1.getSelectedItem().toString();
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Cree une nouvelle instance parametree de Accueil
	 * @param serveur est le serveur où s'effectuera la connexion
	 */
	public Accueil(Serveur serveur) {
		Accueil.serveur = serveur;
		initComponents();
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Initialisation des composants d'AWT.
	 */
	private void initComponents() {

	        jPanel1 = new javax.swing.JPanel();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jLabel3 = new javax.swing.JLabel();
	        modeConnexion = new javax.swing.JComboBox();
	        login = new javax.swing.JTextField();
	        password = new javax.swing.JPasswordField();
	        connexion = new javax.swing.JButton();
	        jLabel4 = new javax.swing.JLabel();
	        jComboBox1 = new javax.swing.JComboBox();
	        jLabel5 = new javax.swing.JLabel();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

	        jLabel1.setText("Logiciel de gestion de trajets");

	        jLabel2.setText("Login : ");

	        jLabel3.setText("Password :");

	        modeConnexion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Utilisateur", "Administrateur" }));

	        connexion.setText("Se connecter");
	        connexion.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                try {
						connexionActionPerformed(evt);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
	        });

	        jLabel4.setText("Version");

	        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "V1", "V2","V3"}));

	        jLabel5.setText("Se connecter en tant que ...");

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(160, 160, 160)
	                        .addComponent(jLabel1))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(80, 80, 80)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jLabel2)
	                            .addComponent(jLabel3))
	                        .addGap(26, 26, 26)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                            .addComponent(password)
	                            .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)))
	                .addGap(190, 190, 190))
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                .addContainerGap(424, Short.MAX_VALUE)
	                .addComponent(connexion)
	                .addContainerGap())
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addGap(40, 40, 40)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                        .addComponent(jLabel5)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 173, Short.MAX_VALUE)
	                        .addComponent(jLabel4)
	                        .addGap(93, 93, 93))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addComponent(modeConnexion, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
	                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(45, 45, 45))))
	        );
	        jPanel1Layout.setVerticalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jLabel1)
	                .addGap(77, 77, 77)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel2)
	                    .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(32, 32, 32)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel3)
	                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(31, 31, 31)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel4)
	                    .addComponent(jLabel5))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(modeConnexion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(29, 29, 29)
	                .addComponent(connexion)
	                .addContainerGap())
	        );

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                .addContainerGap())
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                .addContainerGap())
	        );

	        pack();
	    }

	private void connexionActionPerformed(java.awt.event.ActionEvent evt) throws IOException, ClassNotFoundException {
		String typeUser = modeConnexion.getSelectedItem().toString();
		if (typeUser.compareTo("Utilisateur") == 0) {
			if (this.login.getText().compareTo("user") == 0) {
				if (this.password.getText().compareTo("user") == 0) {
					Accueil.serveur = Accueil.serveur.charger("V1");
					Accueil.serveur.initReseau();
					Utilisateur u = new Utilisateur(new Client(1,"user","user",Accueil.serveur));
					this.setVisible(false);
					u.setVisible(true);
				}
			} else
				JOptionPane.showMessageDialog(this,
						"Mauvais password et/ou pseudo", "Pas glop...",
						JOptionPane.ERROR_MESSAGE);
		} else if (typeUser.compareTo("Administrateur") == 0) {
			if (this.login.getText().compareTo("root") == 0) {
				if (this.password.getText().compareTo("toor") == 0) {
					Accueil.versionUtilisation = this.jComboBox1.getSelectedItem().toString();
					Administration a = new Administration(new Administrateur("root", "toor", Accueil.serveur));
					a.setVisible(true);
					this.setVisible(false);
				}
			} else
				JOptionPane.showMessageDialog(this,"Mauvais password et/ou pseudo", "Pas glop...", JOptionPane.ERROR_MESSAGE);
		}
	}

	private javax.swing.JButton connexion;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField login;
    private javax.swing.JComboBox modeConnexion;
    private javax.swing.JTextField password;
}